import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
import { useUserStore } from '@/stores/user'

const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login/index.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/components/Layout/index.vue'),
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard/index.vue'),
        meta: { title: '仪表盘', icon: 'DashboardOutlined' }
      },
      {
        path: 'banners',
        name: 'Banners',
        component: () => import('@/views/Content/Banners/index.vue'),
        meta: { title: 'Banner管理', icon: 'PictureOutlined' }
      },
      {
        path: 'products',
        name: 'Products',
        component: () => import('@/views/Content/Products/index.vue'),
        meta: { title: '产品管理', icon: 'AppstoreOutlined' }
      },
      {
        path: 'cases',
        name: 'Cases',
        component: () => import('@/views/Content/Cases/index.vue'),
        meta: { title: '案例管理', icon: 'ProjectOutlined' }
      },
      {
        path: 'solutions',
        name: 'Solutions',
        component: () => import('@/views/Content/Solutions/index.vue'),
        meta: { title: '解决方案', icon: 'BulbOutlined' }
      },
      {
        path: 'news',
        name: 'News',
        component: () => import('@/views/Content/News/index.vue'),
        meta: { title: '新闻管理', icon: 'FileTextOutlined' }
      },
      {
        path: 'jobs',
        name: 'Jobs',
        component: () => import('@/views/Content/Jobs/index.vue'),
        meta: { title: '招聘管理', icon: 'TeamOutlined' }
      },
      {
        path: 'service-packages',
        name: 'ServicePackages',
        component: () => import('@/views/Content/ServicePackages/index.vue'),
        meta: { title: '技术服务套餐', icon: 'GoldOutlined' }
      },
      {
        path: 'custom-packages',
        name: 'CustomPackages',
        component: () => import('@/views/Content/CustomPackages/index.vue'),
        meta: { title: '定制开发套餐', icon: 'CodeOutlined' }
      },
      {
        path: 'package-features',
        name: 'PackageFeatures',
        component: () => import('@/views/Content/PackageFeatures/index.vue'),
        meta: { title: '套餐功能对照', icon: 'TableOutlined' }
      },
      {
        path: 'faqs',
        name: 'FAQs',
        component: () => import('@/views/Content/FAQs/index.vue'),
        meta: { title: '常见问题', icon: 'QuestionCircleOutlined' }
      },
      {
        path: 'users',
        name: 'Users',
        component: () => import('@/views/User/index.vue'),
        meta: { title: '用户管理', icon: 'UserOutlined' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  if (to.path !== '/login' && !userStore.token) {
    next('/login')
  } else if (to.path === '/login' && userStore.token) {
    next('/')
  } else {
    next()
  }
})

export default router

